package com.buddy.qreport.report.service.impl;

import com.buddy.qreport.common.mybatis.entity.Page;
import com.buddy.qreport.query.dao.IQueryMapper;
import com.buddy.qreport.report.dao.IReportMapper;
import com.buddy.qreport.report.entity.ReportAlbumEntity;
import com.buddy.qreport.report.entity.ReportEntity;
import com.buddy.qreport.report.entity.ReportRelationEntity;
import com.buddy.qreport.report.service.IReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service("QREPORT_ReportService")
public class ReportServiceImpl implements IReportService {

    @Autowired
    private IReportMapper reportMapper;

    @Autowired
    private IQueryMapper queryMapper;

    @Transactional
    @Override
    public void addReport(ReportEntity reportEntity) {

        reportMapper.insert(reportEntity);

        if(reportEntity.getReportId() != null){

            if(reportEntity.getReportRelations() != null && !reportEntity.getReportRelations().isEmpty()){

                reportMapper.insertRelations(reportEntity.getReportRelations(),reportEntity.getReportId());
            }
        }
    }

    @Override
    public Page<ReportEntity> getReportlist(Page page){

        if(page != null){

            reportMapper.selectReport(page);

        }

        return page;
    }

    @Override
    public ReportEntity getOne(String reportId) {
        ReportEntity reportEntity =  reportMapper.selectOne(reportId);

        reportEntity.setReportRelations(reportMapper.selectRelations(reportId));

        return reportEntity;
    }

    @Override
    public List<ReportRelationEntity> getRelations(String reportId) {

        ReportEntity reportEntity = this.getOne(reportId);

        if(reportEntity != null) {

            List<ReportRelationEntity> relationEntities = reportEntity.getReportRelations();

            if(relationEntities != null){

                for(ReportRelationEntity entity : relationEntities){

                    entity.setResultColumn(queryMapper.selectOneColumn(entity.getRelationColumn()));

                }

                return relationEntities;
            }


        }

        return Collections.EMPTY_LIST;
    }

    @Override
    public Page<ReportAlbumEntity> getReportAlbum(Page page) {

        reportMapper.selectReportAlbumList(page);

        return page;
    }

    @Override
    public void saveOrUpdateReportAlbum(ReportAlbumEntity entity) {

        if(entity.getAlbumId() == null || "".equals(entity.getAlbumId())){

            reportMapper.saveReportAlbum(entity);
        }else{
            reportMapper.updateReportAlbum(entity);
        }
    }

    @Override
    public ReportAlbumEntity getOneReportAlbum(String id) {
        return reportMapper.selectOneReportAlbum(id);
    }

    @Override
    public void deleteReport(String reportId) {
        reportMapper.deleteReport(reportId);
    }

    @Override
    public void deleteAlbum(String albumId) {
        reportMapper.deleteAlbum(albumId);
    }

}
